package com.css.fxfzxczh.modules.lossAssessment.resetPrice.service.impl;

import com.css.fxfzxczh.base.dict.service.DictService;
import com.css.fxfzxczh.constants.YNEnum;
import com.css.fxfzxczh.modules.lossAssessment.resetPrice.entity.ResetPrice;
import com.css.fxfzxczh.modules.lossAssessment.resetPrice.repository.ResetPriceRepository;
import com.css.fxfzxczh.modules.lossAssessment.resetPrice.service.ResetPriceService;
import com.css.fxfzxczh.util.CreateGson;
import com.css.fxfzxczh.util.PlatformDateUtils;
import com.css.fxfzxczh.web.PlatformSessionContext;
import com.css.fxfzxczh.zcpt.sys.entity.SUser;
import com.css.fxfzxczh.zcpt.sys.service.SUserService;
import org.apache.poi.hssf.usermodel.HSSFDataFormatter;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import static com.css.fxfzxczh.util.PoiExcelUtil.getWorkBook;

/**
 * @Author: zhangSongRui
 * @CreateTime: 2023/02/22
 * @Description: 设定重置单价
 */
@Service
public class ResetPriceServiceImpl implements ResetPriceService {

    @Resource
    private ResetPriceRepository resetPriceRepository;
    @Resource
    SUserService sUserService;
    @Resource
    DictService dictService;

    //分页查询
    @Override
    public Map<String, Object> getPage(int curPage, int pageSize, String xsemId) {
        return resetPriceRepository.getPage(curPage, pageSize,xsemId);
    }

    //导入
    @Override
    public String reportImport(InputStream file, String xsemId) {
        try {
            Workbook workbook = WorkbookFactory.create(file);
            List<ResetPrice> list = getList(workbook, xsemId);
            for (ResetPrice resetPrice : list) {
                resetPriceRepository.batchSave(resetPrice);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "导入成功";
    }

    @Override
    public void deleteById(List<String> ids) {
        resetPriceRepository.deleteById(ids);
    }

    @Override
    public void updateById(ResetPrice param) {
        param.setUpdateUser(PlatformSessionContext.getUserID());
        param.setUpdateTime(PlatformDateUtils.getCurrentTimestamp());
        resetPriceRepository.updateById(param);
    }

    @Override
    public ResetPrice findById(String id) {
        return resetPriceRepository.findById(id);
    }

    @Override
    public List<ResetPrice> getResetPriceByXsemId(String xsemId) {
        return resetPriceRepository.getResetPriceByXsemId(xsemId);
    }

    //获取list
    private List<ResetPrice> getList(Workbook workbook, String xsemId) {
        String userID = PlatformSessionContext.getUserID();
        String user = sUserService.getSUser(userID);
        SUser sUser = CreateGson.createGson().fromJson(user, SUser.class);
        //构筑物部分
        List<ResetPrice> list = new ArrayList<>();
        if (workbook != null) {
            //循环除了第一行的所有行
            /** 得到第二个sheet */
            Sheet sheet = workbook.getSheetAt(0);
            /** 得到Excel的行数 */
            int totalRows = sheet.getPhysicalNumberOfRows();
            HSSFDataFormatter hSSFDataFormatter = new HSSFDataFormatter();
            /** 循环Excel的行 从第七行开始*/
            for (int r = 2; r < totalRows; r++) {
                Row row = sheet.getRow(r);
                if (row == null) {
                    continue;
                }
                //值转字典code
                String s = dictService.getKeyByDictCodeAndValue("jglx", hSSFDataFormatter.formatCellValue(row.getCell(0)));
                ResetPrice up= resetPriceRepository.queryTypeByXsemId(s,xsemId);
                //如果该事件类型下有数据则覆盖;
                if (up!=null){
                    up.setReporter(sUser.getUserName());
                    up.setResetPrice(hSSFDataFormatter.formatCellValue(row.getCell(1)));
                    up.setUpdateUser(userID);
                    up.setUpdateTime(PlatformDateUtils.getCurrentTimestamp());
                    resetPriceRepository.updateById(up);
                    continue;
                }
                ResetPrice resetPrice= new ResetPrice();
                //主键id
                resetPrice.setId(UUID.randomUUID().toString().replaceAll("-", ""));
                //地震事件id
                resetPrice.setXsemId(xsemId);
                //结构类型
                resetPrice.setStructureType(s);
                //重置单价
                resetPrice.setResetPrice(hSSFDataFormatter.formatCellValue(row.getCell(1)));
                //上报人
                resetPrice.setReporter(sUser.getUserName());
                //上报时间
                resetPrice.setReporterTime(PlatformDateUtils.getCurrentTimestamp());
                //创建人
                resetPrice.setCreateUser(userID);
                //0正常1删除
                resetPrice.setDelFlag(YNEnum.N.toString());
                //创建时间
                resetPrice.setCreateTime(PlatformDateUtils.getCurrentTimestamp());
                list.add(resetPrice);
            }
        }
        return list;
    }
}
